package com.xzx.masterslave.masterslave.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * mybatis 配置类
 * @author admin
 */
@Configuration
@ConditionalOnClass({EnableTransactionManagement.class})
@Import(DataSourceConfig.class)
@MapperScan(basePackages = {"com.xzx.masterslave.masterslave.mapper"})
@Slf4j
public class MybatisConfig {
    @Value("${spring.datasource.type}")
    private Class<? extends DataSource> dataSouceType;
    @Value("${datasource.readSize}")
    private String dataSourceSize;
    @Bean
    @ConditionalOnMissingBean
    public SqlSessionFactory sqlSessionFactory(ApplicationContext ac) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(roundRobinDataSouceProxy(ac));
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mappings/**/*.xml"));
        sqlSessionFactoryBean.setTypeAliasesPackage("com.xzx.masterslave.masterslave.entity");
        sqlSessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public AbstractRoutingDataSource roundRobinDataSouceProxy(ApplicationContext applicationContext){
        int size = Integer.parseInt(dataSourceSize);
        log.info("从库size===>"+size);
        MyAbstrctRoutingDataSource myAbstrctRoutingDataSource = new MyAbstrctRoutingDataSource(size);
        //获取写数据源
        DataSource writeDataSource = (DataSource) applicationContext.getBean("writeDatasource");
        //获取多个从数据源
        List<DataSource> listReadDataSource = (List<DataSource>) applicationContext.getBean("readDataSources");

        Map<Object,Object> dataSourceMap = new HashMap<>(16);

        for (int i = 0; i <listReadDataSource.size(); i++) {
            dataSourceMap.put(i, listReadDataSource.get(i));
        }
        myAbstrctRoutingDataSource.setDefaultTargetDataSource(writeDataSource);
        myAbstrctRoutingDataSource.setTargetDataSources(dataSourceMap);
        return myAbstrctRoutingDataSource;
     }




}
